#include "stdafx.h"

#include "input/keyboard.h"
#include "core/event.h"
#include "core/logger.h"

namespace natrium {
	namespace input {
		Keyboard::Keyboard() {
			core::EventChannel chan;

			chan.add<KeyPressed>(this);
			chan.add<KeyReleased>(this);

			for (auto& key: mKeyState)
				key = false;
		}

		Keyboard::~Keyboard() {
		}

		void Keyboard::glfwKeyboardCallback(int key, int state) {
			static core::EventChannel chan;

			switch (state) {
			case GLFW_PRESS:
				chan.broadcast(KeyPressed(key));
				break;

			case GLFW_RELEASE:
				chan.broadcast(KeyReleased(key));
				break;

			default:
				gLog.debug() << "[Keyboard::glfwKeyboardCallback] Unhandled glfw state: " << state;
				break;
			}
		}

		Keyboard::KeyPressed::KeyPressed(int key):
			mKey(key)
		{
		}

		Keyboard::KeyReleased::KeyReleased(int key):
			mKey(key)
		{
		}

		void Keyboard::operator()(const KeyPressed& kp) {
			assert(kp.mKey < GLFW_KEY_LAST);
			mKeyState[kp.mKey] = true;
		}

		void Keyboard::operator()(const KeyReleased& kr) {
			assert(kr.mKey < GLFW_KEY_LAST);
			mKeyState[kr.mKey] = false;
		}
	}
}